		$(function(){
			$('#cropbox').Jcrop({
				bgColor: "transparent",
				aspectRatio: 1,
//				onSelect: updateCoords,
//				onChange: showCoords,
				setSelect: [ 0, 0, $(this).width(), $(this).height()],
				onChange: showPreview,
				onSelect: showPreview
			});
		});
		function showPreview(coords)
		{
			if (parseInt(coords.w) > 0)
			{
				var rx = 100 / coords.w;
				var ry = 100 / coords.h;

				jQuery('#preview').css({
					width: Math.round(rx * $('#cropbox').width()) + 'px',
						height: Math.round(ry * $('#cropbox').height()) + 'px',
					marginLeft: '-' + Math.round(rx * coords.x) + 'px',
					marginTop: '-' + Math.round(ry * coords.y) + 'px'
				});
			}
			updateCoords(coords);
		}
		function updateCoords(c)
		{
			$('#x').val(c.x);
			$('#y').val(c.y);
			$('#w').val(c.w);
			$('#h').val(c.h);
		};

		function checkCoords()
		{
			if (parseInt($('#w').val())) return true;
			alert(pagecontent[13]);
			return false;
		};

		function upload_logo_show()
		{
			$('#upload_logo').slideToggle();

		}
		function change_color(place)
		{
			$("#color_picker").attr("color_num",place);
			create_message_box();
			$("#msg").html("<div style=\"background-color:#578229; padding: 10px;\"><strong>"+pagecontent[14]+" "+place+"</strong></div><div style=\"background-color:#578229; padding: 0 10px;\" id=\"popup_color_picker\">"+$("#color_picker").html()+"</div>");
			$("#msg").append("<div style=\"background-color:#578229; text-align: center; padding-bottom: 10px;\"><a href=\"javascript:close_message_box()\" class=\"button\"><span class=\"button_border\">"+global_content["cancel"]+"</span></a></div>");
			$(".select_color").each(function(){
				$(this).mouseover(function(){
					$(this).addClass("hover");
				})
				.mouseout(function(){
					$(this).removeClass("hover");
				})
			});
			show_message_box();
		}

		function select_color(elem)
		{
			var color = $(elem).attr("color_val");
			var place = $("#color_picker").attr("color_num");
			$.post("/ajax/settings_set.ajax.php",{"setting": "club_color"+place,"show":color},function(data){
				if(data != null)
				{
					var $prev = $(".select_color"+place);
					$prev.removeClass("selected");
					$prev.removeClass("select_color"+place);
					$(elem).addClass("selected");
					$(elem).addClass("select_color"+place);
					$("#color_picker").html($("#popup_color_picker").html());
					$(elem).removeClass("hover");
					close_message_box();
					$("#selected_color"+place).css("background","#"+color);
					$("#color_picker").hide();
					$("#selected_color"+place+"_message").html("Saved");
				}
			},"json");
		}
		//Function to convert hex format to a rgb color
		function rgb2hex(rgb) {
			rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
			return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
			function hex(x) {
				var hexDigits = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; 
				return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
			}
		}
		function set_club_color(place,color)
		{
			$.post("/ajax/settings_set.ajax.php",{"setting": "club_color"+place,"show":color},function(data){},"json");
		}

	function show_language_select() {
		$("#show_language_select_link").hide(200);
		$("#language_select").show(200);
	}

	function change_language(lang){
		lang = lang || $("#language_select").val();
		window.location="?language="+lang;
	}

	function show_div() {
		$("#show_change").slideToggle();
		$("#hide_change").slideToggle();
		$("#password").slideToggle();
		$("#formular").focus();
	}

	function banner_submit(col_name) {
		$("#"+col_name+"_checkbox").find(".checkbox_label").find(".checkmark_small").remove();
		var val = $("#"+col_name).is(":checked") ? 1 : 0;
		$.post("/ajax/settings_set.ajax.php",{"setting": col_name,"show":val},function(data){
				$("#"+col_name+"_checkbox").find(".checkbox_label").append("<span class='checkmark_small'></span>");
			});
	}

	function club_desc_pop()
	{
		var html = "<form method=\"post\" action=\"\" id=\"save_club_desc\">"+
			"<fieldset>"+
			"<h2>"+pagecontent[15]+"</h2>"+
			"<input type='hidden' name='save_club_description' value='1' />"+
			"<div class='club_desc_row'><label for=\"manager_name\">"+$(".man_name td:first").html()+"</label><input tabindex='1' type=\"text\" class='emboss' name=\"manager_name\" id=\"manager_name\" value=\""+$(".man_name td:last").html()+"\" /></div>"+
			"<div class='club_desc_row'><label for=\"city\">"+$(".club_city td:first").html()+"</label><input tabindex='2' class='emboss' type=\"text\" name=\"city\" id=\"city\" value=\""+$(".club_city td:last").html()+"\" /></div>"+
			"<div class='club_desc_row'><label for=\"club_nick\">"+$(".club_nick td:first").html()+"</label><input tabindex='3' class='emboss' type=\"text\" name=\"club_nick\" id=\"club_nick\" value=\""+$(".club_nick td:last").html()+"\" /></div>"+
			"<div class='club_desc_row'><label for=\"fan_club_name\">"+$(".fanclub td:first").html()+"</label><input tabindex='4' class='emboss' type=\"text\" name=\"fan_club_name\" id=\"fan_club_name\" value=\""+$(".fanclub td:last").html()+"\" /></div>"+
			"<div class='club_desc_row'><label for=\"stadium_name\">"+$(".stadium td:first").html()+"</label><input tabindex='5' class='emboss' type=\"text\" name=\"stadium_name\" id=\"stadium_name\" value=\""+$(".stadium td:last").html()+"\" /></div>"+
			"<input type='submit' class='display_none' name='submit_now'/>"+
			"</fieldset>"+
			"</form>"+
			"<div class='msgbuttons'><span onclick='javascript:club_desc_save()' tabindex='6' class='button'><span class='button_border'>"+global_content["save"]+"</span></span> "+
			"<span class='button' onclick='javascript:modal.hide()' tabindex='7'><span class='button_border'>"+global_content["cancel"]+"</span></span></div>";
		modal.html(html).show();
		emboss();
	}

	function pop_big_forum_logo(confirm) {
		var html;
		if (confirm) {
			modal.inner().append($(get_loading_img()).addClass("loading")).find(".msgbuttons").hide();
			$.post("/ajax/settings_big_forum_logo.ajax.php",{},function(data){
				modal.inner().find(".loading").remove();
				
				if(data["success"])
				{
					html = pagecontent[141];
					$("#big_forum_logo_enabled").show();
					$("#big_logo_button").hide();
					pro_update();
				}
				else 
				{
					html = pagecontent[142];
					button_disable($("#big_logo_button"));
				}
				
				html += "<div class='msgbuttons'>"
				+ make_button(global_content["ok"], "modal.hide()")
				+"</div>";
				modal.html(html).show();
			},"json");
		
		} else {
			html ="<h2>Big Forum Logo</h2>"
					+pagecontent[143]
					+"<div class='msgbuttons'>"
					+make_button(global_content["ok"], "pop_big_forum_logo(true)")
					+make_button(global_content["cancel"], "modal.hide()")
					+"</div>";
					
					modal.html(html).show();
		}
	// global->club_settings_set eller noget..
		
	}
	
	function create_reserve_pop()
	{
		if (pro_days>15 || is_diam==1)
		{
			var html = "<h2>"+pagecontent[16]+"</h2><p>"+pagecontent[17]+"</p>"
				+"<form id='reserve_create_formula' name='reserve_create_formula' method='post' action='/account/reserve-teams/'>"
				+"<input type='hidden' name='reserve' value='create'></form>"
				+"<div class='msgbuttons'>"
				+make_button(global_content["create"],"reserve_create_formula.submit()")+" "
				+make_button(global_content["cancel"],"modal.hide()")
				+"</div>";
		}
		else
		{
			var html = "<p>"+pagecontent[103]+"</p>"
				+"<div class='msgbuttons'>"
				+make_button(global_content["ok"],"modal.hide()")
				+"</div>";
		}
		modal.html(html).show();
	}

	function delete_reserve_pop()
	{
		modal.html("<h2>"+pagecontent[18]+"</h2><p>"+pagecontent[19]+"</p>"
			+"<form id='reserve_delete_formula' name='reserve_delete_formula' method='post' action='/account/reserve-teams/'>"
			+"<input type='hidden' name='reserve' value='inactivate'></form>"
			+"<div class='msgbuttons'>"
			+make_button(global_content["delete"]+" <img src='/pics/small_red_x.png' />","submit_form('reserve_delete_formula')")
			+" "+make_button(global_content["cancel"],"modal.hide()")
			+"</div>").show();
	}
	function reserve_renew_pop()
	{
		var html ="<h2>"+pagecontent[20]+"</h2><p>"+pagecontent[21]+"</p>"+
		"<form id='reserve_formula' name='reserve_formula' method='post' action='/account/reserve-teams/'>"+
		pagecontent[22]+" "+
		"<select name='months' id='reserves_months'>"+
		"<option value='1'>1</option>"+
		"<option value='2'>2</option>"+
		"<option value='3' selected>3</option>"+
		"<option value='4'>4</option>"+
		"<option value='5'>5</option>"+
		"<option value='6'>6</option>"+
		"<option value='7'>7</option>"+
		"<option value='8'>8</option>"+
		"<option value='9'>9</option>"+
		"<option value='10'>10</option>"+
		"<option value='11'>11</option>"+
		"<option value='12'>12</option>"+
		"</select></form>"+
		"<div class='msgbuttons'><span class='button' onclick='submit_form(\"reserve_formula\");'><span class='button_border' style='padding: 0px 6px'>"+global_content["renew"]+"</span></span> "+
		" <span onclick='modal.hide()' class='button'><span class='button_border' style='padding: 0px 6px'>"+global_content["cancel"]+"</span></span></div>";
		modal.html(html).show();
		$("#reserves_months").selectmenu({
			"width":"100",
			"maxHeight":"250",
			"style":"dropdown"
		});
	}

	function club_desc_save()
	{
		$("#save_club_desc").submit();
		modal.hide();
	}
	function pop_change_fav_player(number,player_id)
	{
		create_message_box();
		$("#msg").html("<div class='std'>"+get_loading_img()+"</div>");
		var $div = $("<div class='std'><h2>"+pagecontent[138]+"</h2></div>");
		show_message_box();
		$.post("/ajax/account_player_list.ajax.php",{},function(data){
			if(data != null)
			{
				var $players = $("<div>");
				var j = 1;
				var players_count = count(data);
				for(var i in data)
				{
					var player = data[i];
					if(player)
					{
						var sel = player["player_id"] == player_id ? " checked" : "";
						$players.append("<input class='less_space' type='radio' name='player_id' id='player"+j+"' value='"+player["player_id"]+"' "+sel+"/><label for='player"+j+"'><img src='/pics/place_dot_"+player["fav_position_icon"]+".png' style='margin-left:4px;margin-right:4px;'>"+" <span player_link='"+player["player_id"]+"'>"+player["name"]+"</span> "+player["recommendation"]+"</label>");
						j++;
					}
				}
				$div.append($players).append("<div class='clear'>");
				$div.append("<div style='margin-top:10px' class='align_center'><a class='button' href='javascript:save_fav_player("+number+");'><span class='button_border' style='padding: 0px 6px'>"+global_content["save"]+"</span></a> "+
		"<a href='javascript:close_message_box()' class='button'><span class='button_border' style='padding: 0px 6px'>"+global_content["cancel"]+"</span></a>"+
		"</div>");
				$("#msg").html($div);
				make_radio("player1");
				activate_player_links($div.find("[player_link]"));
			}
		},"json");
	}
	function save_fav_player(number)
	{
		var player_id = $("[name=player_id]:checked").val();
		$.post("/ajax/settings_set.ajax.php",{"setting":"favourite_player_"+number,"show":player_id},function(){
			page_refresh();
		},"json");
	}

	function calculate_donation()
	{
		var pro = parseInt($("#donate_days").val());
		var calc = pro - Math.ceil(pro/100*10) - 1;
		var days_left = parseInt($("#donate_days_left").val());
		if(pro > (days_left - 2))
		{
			var html = "<ul class='error'><li>"+pagecontent[122]+"</li></ul>";
			button_disable($("#donate_button"));
		}
		else if(pro > 2)
		{
			var html = "<ul><li>"+pc_replace(pagecontent[120],{"[days]":"<strong class='large'>"+pro+"</strong>"})+"</li>"
				+"<li>"+pc_replace(pagecontent[121],{"[days]":"<strong class='large'>"+calc+"</strong>"})+"</li></ul>";
			button_enable($("#donate_button"));
		}
		else
		{
			var html = "<ul class='error'><li>"+pagecontent[123]+"</li></ul>";
			button_disable($("#donate_button"));
		}
		$("#calculation").html(html);

	}
	$(document).ready(function(){
		if($("#donate_days").length > 0)
		{
			calculate_donation();
		}
	});
	function donate_select_club(club_id,sug_id)
	{
		var $club = $("#suggestion_id_"+sug_id);
		$("#donate_club_name").html($club.html()).show();
		$("#donate_club").closest(".emboss_outer").hide();
		$("#donate_club").val(club_id);
		$("#donate_club_name").css("cursor","pointer").unbind()
			.click(function(){
				$(this).html("").hide();
				$("#donate_club").closest(".emboss_outer").show();
			});
		$("#suggest_clubs_box").remove();
	}

	function pop_donate()
	{
		var club_id = parseInt($("#donate_club").val());
		var days = parseInt($("#donate_days").val());
		var name = $("#donate_club_name").html();
		var html="<h2>"+pagecontent[83]+"</h2>";
		if(club_id > 0 && days > 2)
		{
			var calc = days - Math.ceil(days/100*10) - 1;
			html += "<p>"+pc_replace(pagecontent[124],{"[days]":"<strong class='large'>"+days+"</strong>","[club]":"<span class='large'>"+name+"</span>"})+"</p>"
				+pc_replace(pagecontent[125],{"[days]":"<strong class='large'>"+calc+"</strong>","[club]":"<span class='large'>"+name+"</span>"})
				+"<div class='msgbuttons'>"+make_button(pagecontent[86],"post_donate()","big_button large")+" "+make_button(global_content["cancel"],"modal.hide()")+"</div>";
		}
		else
		{
			html+="<p>"+pagecontent[126]+"</p>"
				+"<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()","","ok_button")+"</div>";
		}
		modal.html(html).show();
		$("#ok_button").attr("tabindex","-1").focus();
	}

	function post_donate()
	{
		var club_id = parseInt($("#donate_club").val());
		var days = parseInt($("#donate_days").val());
		modal.transition(get_loading_img(),function(){
			$.post("/ajax/account_pro_donate.ajax.php",{"days":days,"club_id":club_id},function(data){
				if(data != null)
				{
					var html="<h2>"+pagecontent[83]+"</h2>";
					if(data["error"] != "")
					{
						html+="<p class='error'>"+data["error"]+"</p>"
						+"<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()","","ok_button")+"</div>";
					}
					else
					{
						html += "<p>"+data["message"]+"</p>"
						+"<div class='msgbuttons'>"+make_button(global_content["ok"],"page_refresh()","","ok_button")+"</div>";
						body_refresh();
					}
					modal.html(html);
				}
			},"json");
		});
	}

	function pop_change_club_name()
	{
		modal.html(get_loading_img()).show();
		$.post("/ajax/account_change_clubname.ajax.php",{"type":"check"},function(data){
			if(data != null)
			{
				var html = "<h2>"+pagecontent[139]+"</h2>";
				if(data["test_pro"])
				{
					if(data["free"])
					{
						html += "<p>"+pagecontent[127]+"</p>";
					}
					else
					{
						;
							html += "<p>"+pc_replace(pagecontent[128],{"[span]":"<strong class='large'>","[/span]":"</strong>","[days]":20})+"</p>";
					}
					html += "<label for='new_club_name'>"+pagecontent[140]+"</label> <input type='text' maxlength='30' id='new_club_name' name='new_club_name' class='emboss stdwidth' value=''/>"
						+"<input type='hidden'  id='old_club_name' name='old_club_name' value=''/>"
						+"<input type='hidden' id='new_club_free' value='"+data["free"]+"' />"
						+"<p class='error' id='club_name_error' style='display: none'></div>"
						+"<div class='msgbuttons'>"+make_button(global_content["change"],"post_change_club_name(false)")+" "+make_button(global_content["cancel"],"modal.hide()")+"</div>";
				}
				else
				{
					html += "<p class='error'>"+pagecontent[129]+" <a href='/buy-pro/'>"+pagecontent[130]+"</a></p>"
						+"<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()")+"</div>";
				}

				modal.html(html);
				$("#new_club_name").val(data["club_name"]);
				$("#old_club_name").val(data["club_name"]);
				emboss();
			}
		},"json");
	}
	function post_change_club_name(confirmed)
	{
		var old_name = $("#old_club_name").val();
		var new_name = $("#new_club_name").val();
		var free = $("#new_club_free").val();
		if(new_name == "")
		{
			$("#club_name_error").html(pagecontent[131]).show();
			// error
			return false;
		}
		if(new_name == old_name)
		{
			$("#club_name_error").html(pagecontent[132]).show();
			return false;
		}
		if(confirmed)
		{ // Do the real post and change the name
			modal.transition(get_loading_img(),function(){
				$.post("/ajax/account_change_clubname.ajax.php",{"type":"save","name":new_name},function(data){
					if(data != null)
					{
						var html ="<h2>"+pagecontent[139]+"</h2>";

						if(data["error"] == "")
						{
							html += "<p>"+pc_replace(pagecontent[133],{"[name]":"<strong class='large'>"+new_name+"</strong>"})+"</p>";
						}
						else
						{
							html += "<p class='error'>"+data["error"]+"</p>";
						}
						body_refresh();
						html += "<div class='msgbuttons'>"+make_button(global_content["ok"],"page_refresh()")+"</div>";
						modal.html(html);
					}
				},"json");
			});
		}
		else
		{ // Do character check and confirm with user
			modal.transition(get_loading_img(),function(){
				$.post("/ajax/account_change_clubname.ajax.php",{"type":"test","name":new_name},function(data){
					if(data != null)
					{
						var html ="<h2>"+pagecontent[139]+"</h2>";
						if(data["name"] == "")
						{
							html += "<p>"+pagecontent[134]+"</p>";
							html += "<div class='msgbuttons'>"+make_button(global_content["ok"],"modal.hide()")+"</div>";
							modal.html(html);
							return false;
						}
						html += "<p>"+pc_replace(pagecontent[135],{"[name]":"<strong class='large'>"+data["name"]+"</strong>"});+"</p>";
						html += "<input type='hidden' id='new_club_name' value='"+data["name"]+"' />";
						html += "<input type='hidden' id='old_club_name' value='"+old_name+"' />";
						html += "<p class='error' id='club_name_error' style='display: none'></div>";
						if(free == "true")
						{
							html += "<p>"+pc_replace(pagecontent[136],{"[span]":"<strong class='large'>","[/span]":"</strong>","[days]":20})+"</p>";
						}
						else
						{
							html += "<p>"+pc_replace(pagecontent[137],{"[span]":"<strong class='large'>","[/span]":"</strong>","[days]":20})+"</p>";
						}
						html += "<p class='bold'>"+pagecontent[75]+"</p>";
						html += "<div class='msgbuttons'>"+make_button(global_content["change"],"post_change_club_name(true)")+" "+make_button(global_content["cancel"],"modal.hide()")+"</div>";
						modal.html(html);
						$("#new_club_name").val(new_name);
						$("#old_club_name").val(old_name);
					}
				},"json");
			});
		}
	}
	function update_stadium_view(x)
	{
		if(x == "foreground")
		{
			var val = $("[name=stadium_foreground]:checked").val();
			$("#stadium_foreground_img").attr("src","/pics/facilities/backgrounds/"+val);
		}
		else
		{
			var val = $("[name=stadium_background]:checked").val();
			$("#stadium_background_img").attr("src","/pics/facilities/backgrounds/"+val);
		}
	}
	function save_stadium_background()
	{
		$("#stadium_bg_saved").html(get_loading_img());
		var fg = $("[name=stadium_foreground]:checked").attr("key");
		var bg = $("[name=stadium_background]:checked").attr("key");
		$.post("/ajax/account_stadium_bg.ajax.php",{"bg":bg,"fg":fg},function(data){
			if(data != null)
			{
				$("#stadium_bg_saved").html("<img src='/pics/circle_check.png' />");
			}
		},"json");
	}
	
	function saveWings(wingId)
	{

		$.post("/ajax/account_wings.ajax.php",{"wings":wingId},function(data){
			if(data != null)
			{
				var html;
			
				html += "<p>Fly baby fly! You have new wings :D</p>";
				html += "<div class='msgbuttons'>"
					+make_button(global_content["ok"],"refresh_page()","","",2)
					+"</div>";
				body_refresh();
			
				modal.html(html).show();
				emboss();
			}
		},"json");

	}
	
	function pop_reserves_name(club_id,save)
	{
		var html = "<h2>"+pagecontent[109]+"</h2>";
		if(save)
		{
			var name = $("#reserves_name").val();
			if(name == "")
			{
				return false;
			}
			var nickname = $("#reserves_nick").val();
			modal.transition(get_loading_img(),function(){
				$.post("/ajax/account_reserves.ajax.php",{"type":"save_name","club_id":club_id,"name":name,"nickname":nickname},function(data){
					if(data != null)
					{
						if(data["error"] == "")
						{
							html += "<p>"+pc_replace(pagecontent[110],{"[club_name]":"<strong class='large'>"+data["name"]+"</strong>"})+"</p>";
							html += "<div class='msgbuttons'>"
								+make_button(global_content["ok"],"refresh_page()","","",2)
								+"</div>";
							body_refresh();
						}
						else
						{
							html += "<p class='error'>"+data["error"]+"</p>";
						}
							modal.html(html);
							emboss();
					}
				},"json");
			});
		}
		else
		{
			modal.html(get_loading_img()).show();
			$.post("/ajax/account_reserves.ajax.php",{"type":"get_name","club_id":club_id},function(data){
				if(data != null)
				{
					if(data["error"] == "")
					{
						html += "<label for='reserves_name'>"+pagecontent[111]+"</label> <input type='text' name='reserves_name' id='reserves_name' value='"+data["name"]+"' class='emboss stdwidth' focus tabindex='1' maxlength='30'/>";
						html += "<br /><label for='reserves_nick'>"+pagecontent[59]+"</label> <input type='text' name='reserves_nick' id='reserves_nick' value='"+data["nickname"]+"' class='emboss stdwidth' focus tabindex='1' maxlength='30'/>";
						html += "<div class='msgbuttons'>"
							+make_button(global_content["save"],"pop_reserves_name("+club_id+",true)","","",2)+" "
							+make_button(global_content["cancel"],"modal.hide()","","",3)
							+"</div>";
					}
					else
					{
						html += "<p class='error'>"+data["error"]+"</p>";
					}
						modal.html(html);
						emboss();
				}
			},"json");
		}
	}
function submit_password()
{
	return true;
}
function set_advanced_mode()
{
	$("#advanced_mode_checkbox .advanced_mode_check").hide();
	var val = $("#advanced_mode").is(":checked") ? 1 : 0;
	$.post("/ajax/account_advanced.ajax.php",{"setting":"advanced_mode","show":val},function(data){
		if(data)
		{
			$("#advanced_mode_checkbox .advanced_mode_check").show();
		}
	},"json");
}
